<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>AJAX 获取Json数据</title>
    <style>
      #result {
        width: 300px;
        height: 100px;
        border: solid 1px;
      }
    </style>
  </head>
  <body>
    <button id="btn1">获取Json数据</button>
    <div id="result"></div>

    <script>
      const btn = document.getElementById("btn1");
      const result = document.getElementById("result");

      btn.onclick = function () {
        const xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://localhost:8080/json')
        xhr.responseType='json'
        xhr.send('a=100&b=200&c=300');
        xhr.onreadystatechange = function(){
            if(xhr.readyState === 4){
                if(xhr.status >= 200 && xhr.status < 300){
                    console.log(xhr.status);
                    console.log(xhr.statusText)
                    console.log(xhr.getAllResponseHeaders());
                    console.log(xhr.response);
                    const data = xhr.response;
                    result.innerHTML = data.toString();
                } else {

                }
            }
        }
      };

    </script>
  </body>
</html>
